<?php

class Admin_AccountController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('zendblog')->headTitle('admin')->setSeparator(' :: ');
        $this->view->title = 'admin';
    }

    public function indexAction() {
        $this->view->username = Zend_Auth::getInstance()->getIdentity()->username;
        $this->view->name = Zend_Auth::getInstance()->getIdentity()->name;
        $this->view->email = Zend_Auth::getInstance()->getIdentity()->email;
    }

    public function editAction() {
        $form = new Application_Form_ProfileEdit();

        if (!$this->getRequest()->isPost()) {
            $table = new Application_Model_DbTable_Users();
            $authors = $table->find(Zend_Auth::getInstance()->getIdentity()->id);
            $form->setElementFilters(array()); // disable all element filters
            $this->_repopulateForm($form, $authors->current());
            $this->view->form = $form;
            return;
        } elseif (!$form->isValid($_POST)) {
            $this->view->isInvalid = true;
            $this->view->form = $form->setDefault('username', Zend_Auth::getInstance()->getIdentity()->username);
            return;
        }

        $values = $form->getValues();
        $table = new Application_Model_DbTable_Users();

        $data = array(
            'name' => $values['name'],
            'email' => $values['email']
        );
        $table->update($data,
                $table->getAdapter()->quoteInto('id = ?', $values['id'])
        );
        $storage = Zend_Auth::getInstance()->getStorage()->read();
        $storage->name = $values['name'];
        $storage->email = $values['email'];
        Zend_Auth::getInstance()->getStorage()->write($storage);
        $this->_forward('index');
    }

    public function changeAction() {
        $form = new Application_Form_ChangePassword();

        if (!$this->getRequest()->isPost()) {
            $table = new Application_Model_DbTable_Users();
            $values = array(
                'id' => Zend_Auth::getInstance()->getIdentity()->id
            );
            $form->populate($values);
            $this->view->form = $form;
            return;
        } elseif (!$form->isValid($_POST)) {
            $this->view->isInvalid = true;
            $this->view->form = $form;
            return;
        }

        $table = new Application_Model_DbTable_Users();

        $data = array(
            'password' => hash('SHA256', $form->getValue('password'))
        );
        $table->update($data,
                $table->getAdapter()->quoteInto('id = ?', $form->getValue('id'))
        );
        $this->_forward('index');
    }

    protected function _repopulateForm($form, $author) {
        $values = array(
            'username' => Zend_Auth::getInstance()->getIdentity()->username,
            'name' => $author->name,
            'email' => $author->email,
            'id' => Zend_Auth::getInstance()->getIdentity()->id
        );
        $form->populate($values);
    }

}

